package com.shundr.shipper.truck.adapter;

import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.shundr.shipper.R;
import com.shundr.shipper.common.util.LImageLoader;
import com.shundr.shipper.common.util.ToastHelper;
import com.shundr.shipper.frame.http.LHttpRequest;
import com.shundr.shipper.frame.util.StringUtil;
import com.shundr.shipper.truck.http.UserTruckRequest;
import com.shundr.shipper.truck.model.TruckInfo;

/**
 * 我的车辆列表适配器
 * TruckAdapter
 * 
 * @author larry 2015-4-27 下午4:30:10 
 * @version 1.0.0
 *
 */
public class FamTruckAdapter extends BaseAdapter {
	private List<TruckInfo> mList;
	private Context mContext;
	private ImageLoader mImageLoader;
	
	private OnFamAddListener addListener;

	public FamTruckAdapter(Context context, List<TruckInfo> list) {
		this.mContext = context;
		this.mList = list;
		this.mImageLoader = ImageLoader.getInstance();
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHold viewHold = null;
		if(convertView == null){
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_fam_truck_info,null);
			viewHold = new ViewHold();
			viewHold.TruckCount = (TextView) convertView.findViewById(R.id.tv_type);
			viewHold.TruckNum = (TextView) convertView.findViewById(R.id.tv_code);
			viewHold.TruckPicture = (ImageView) convertView.findViewById(R.id.iv_item_truck_picture);
			viewHold.btnAdd = (Button) convertView.findViewById(R.id.btn_add);
			convertView.setTag(viewHold);
		}else{
			viewHold = (ViewHold) convertView.getTag();
		}
		try {
			final TruckInfo info = mList.get(position);
			if (!StringUtil.isNull(info.getTruckPlateNumber())) {
				viewHold.TruckNum.setText(info.getTruckPlateNumber());
			}
			String type = info.getTruckType() == null ? "" : (info.getTruckType() + " ");
			if(StringUtil.isEqual(info.getTruckLength(), "23")){
				viewHold.TruckCount.setText(type+info.getTruckCapacity() + " 吨 " + info.getTruckLength() + "米以上" );
			}else{
				viewHold.TruckCount.setText(type+info.getTruckCapacity() + " 吨 " + info.getTruckLength() + "米" );
			}
			mImageLoader.displayImage(info.getTruckPic(), viewHold.TruckPicture,LImageLoader.mOptions);
			viewHold.btnAdd.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(final View v) {
					new UserTruckRequest(mContext, new Handler(){
						@Override
						public void handleMessage(Message msg) {
							switch (msg.what) {
							case UserTruckRequest.ADD_FAM_TRUCK_SUCCESSED:
								try {
									ToastHelper.toastShort(mContext,msg.obj.toString());
									v.setBackgroundResource(R.drawable.item_common_gray_selector);
									v.setClickable(false);
									addListener.onAdd();
								} catch (Exception e) {
									e.printStackTrace();
								}
								break;
							case LHttpRequest.REQUEST_FAILED:
								try {
									ToastHelper.toastShort(mContext,msg.obj.toString());
								} catch (Exception e) {
									e.printStackTrace();
								}
								break;
							default:
								break;
							}
							super.handleMessage(msg);
						}
					}).addFamTruck(info.getTruckId());
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
		return convertView;
	}

	class ViewHold {
		/**
		 * 车的序号
		 */
		TextView TruckCount;
		/**
		 * 车牌号码
		 */
		TextView TruckNum;
		/**
		 * 车辆照片
		 */
		ImageView TruckPicture;
		Button btnAdd;
	}
	
	public void setAddListener(OnFamAddListener addListener) {
		this.addListener = addListener;
	}

	public interface OnFamAddListener{
		public void onAdd();
	}
}
